/**
 *  封装请求的方法
 */
import { message } from "antd";
import axios from "axios";
import { CookieUtil } from "../utils/caches";

// 设置cross跨域 并设置访问权限 允许跨域携带cookie信息
axios.defaults.withCredentials = true;
const headers = {
  // 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  'Content-Type': 'application/json; charset=utf-8'
}

// 请求 request 拦截器，在这里，我们给每个请求都加 sessionId
axios.interceptors.request.use(
  request => {
    const sessionId = CookieUtil.get('sessionId') || "";
    request.headers["SessionId"] = sessionId;
    return request;
  },
  error => {
    return Promise.reject(error);
  }
);


// 响应 response 拦截器
axios.interceptors.response.use(
   response => {
    return response.data;
  },
  error => {
    message.error(error?.message || "接口错误");
    return error && Promise.reject(error);
  }
);


export const request = {
  get: (url, data = {}) => axios.get(url, { params: data }),
  post: (url, data = {}) => {
    return axios.post(url, data, { headers })
  }
}







